package dao;


import pojo.entity.Admin;
import utils.DbUtil;

import java.sql.*;

import static management.AdminManagement.ADMINNAME;

public interface AdminDao {
    //增加管理员
    public static void addAdmin(Admin admin) throws SQLException {
        //获取连接
        Connection conn = DbUtil.getConnection();
        //sql
        String sql = "INSERT INTO admin(name, username, password, status,online,last_action_time)"
                + "values(" + "?,?,?,?,?,?)";
        //预编译
        PreparedStatement ptmt = conn.prepareStatement(sql); //预编译SQL，减少sql执行

        //传参
        ptmt.setString(1, admin.getName());
        ptmt.setString(2, admin.getUsername());
        ptmt.setString(3, admin.getPassword());
        ptmt.setInt(4, admin.getStatus());
        ptmt.setInt(5, admin.getOnline());
        ptmt.setTimestamp(6, java.sql.Timestamp.valueOf(java.time.LocalDateTime.now()));

        //执行
        ptmt.executeUpdate();
    }

    //修改管理员
    public static void updateAdmin(Admin admin) throws SQLException {
        //获取连接
        Connection conn = DbUtil.getConnection();
        //sql, 每行加空格
        String sql = "UPDATE admin" +
                " set name=?, username=?, password=?,status=?,online = ?,last_action_time=?"+
                " where id=?";
        //预编译
        PreparedStatement ptmt = conn.prepareStatement(sql); //预编译SQL，减少sql执行

        //传参
        ptmt.setString(1, admin.getName());
        ptmt.setString(2, admin.getUsername());
        ptmt.setString(3, admin.getPassword());
        ptmt.setInt(4, admin.getStatus());
        ptmt.setInt(5, admin.getOnline());
        ptmt.setTimestamp(6, java.sql.Timestamp.valueOf(java.time.LocalDateTime.now()));
        ptmt.setInt(7, admin.getId());

        //执行
        ptmt.executeUpdate();
    }


    //删除管理员
    public static void delAdmin(Integer id) throws SQLException {
        //获取连接
        Connection conn = DbUtil.getConnection();
        //sql, 每行加空格
        String sql = "delete from admin where id=?";
        //预编译SQL，减少sql执行
        PreparedStatement ptmt = conn.prepareStatement(sql);

        //传参
        ptmt.setInt(1, id);

        //执行
        ptmt.execute();
    }

    //根据id查询管理员信息
    public static Admin getAdminById(Integer id) throws SQLException {
        Admin admin = null;
        //获取连接
        Connection conn = DbUtil.getConnection();
        //sql, 每行加空格
        String sql = "select * from  admin where id=?";
        //预编译SQL，减少sql执行
        PreparedStatement ptmt = conn.prepareStatement(sql);
        //传参
        ptmt.setInt(1, id);
        //执行
        ResultSet rs = ptmt.executeQuery();
        while(rs.next()){
            admin = new Admin();
            admin.setId(rs.getInt("id"));
            admin.setName(rs.getString("name"));
            admin.setUsername(rs.getString("username"));
            admin.setPassword(rs.getString("password"));
            admin.setStatus(rs.getInt("status"));
            admin.setOnline(rs.getInt("online"));
            admin.setLastActionTime(rs.getTimestamp("last_action_time").toLocalDateTime());
        }
        return admin;
    }

    //根据用户名查询管理员信息
    public static Admin getAdminByUsername(String username) throws SQLException {
        Admin admin = null;
        //获取连接
        Connection conn = DbUtil.getConnection();
        //sql, 每行加空格
        String sql = "select * from  admin where username=?";
        //预编译SQL，减少sql执行
        PreparedStatement ptmt = conn.prepareStatement(sql);
        //传参
        ptmt.setString(1, username);
        //执行
        ResultSet rs = ptmt.executeQuery();
        while(rs.next()){
            admin = new Admin();
            admin.setId(rs.getInt("id"));
            admin.setName(rs.getString("name"));
            admin.setUsername(rs.getString("username"));
            admin.setPassword(rs.getString("password"));
            admin.setStatus(rs.getInt("status"));
            admin.setOnline(rs.getInt("online"));
            admin.setLastActionTime(rs.getTimestamp("last_action_time").toLocalDateTime());
        }
        return admin;
    }
    //根据用户名设置管理员是否在线
    public static void setAdminOnlineStatus(String username, Integer online) throws SQLException {
        //获取连接
        Connection conn = DbUtil.getConnection();
        //sql, 每行加空格
        String sql = "UPDATE admin" +
                " set online = ?"+
                " where username=?";
        //预编译
        PreparedStatement ptmt = conn.prepareStatement(sql); //预编译SQL，减少sql执行

        //传参
        ptmt.setInt(1, online);
        ptmt.setString(2, username);
        //执行
        ptmt.execute();
    }

    public static boolean isAdminOfflineInDB() throws SQLException {
        String sql = "SELECT online FROM admin WHERE username=?";
        Connection c = DbUtil.getConnection();
        PreparedStatement ps = c.prepareStatement(sql);
        ps.setString(1, ADMINNAME);
        ResultSet rs = ps.executeQuery();
        return rs.next() && rs.getInt(1) == 0;   // 0=离线
    }

}
